package com.ytjj.qmyx.mall.service.thirdpay;

import com.alibaba.fastjson.JSONObject;
import com.upay.sdk.ConfigurationUtils;
import com.upay.sdk.entity.Payer;
import com.upay.sdk.entity.ProductDetail;
import com.upay.sdk.exception.HmacVerifyException;
import com.upay.sdk.exception.RequestException;
import com.upay.sdk.exception.ResponseException;
import com.upay.sdk.exception.UnknownException;
import com.upay.sdk.executer.ResultListenerAdpater;
import com.upay.sdk.onlinepay.builder.OrderBuilder;
import com.upay.sdk.onlinepay.builder.OrderResponse;
import com.upay.sdk.onlinepay.builder.QueryBuilder;
import com.upay.sdk.onlinepay.builder.RefundBuilder;
import com.upay.sdk.onlinepay.executer.OnlinePayOrderExecuter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.configuration.MapConfiguration;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class UpayTest {

    public static Map<String, String> doUnifiedOrder(){

        OrderBuilder builder = new OrderBuilder("892239469");
        builder.setRequestId("PAY2024031511311333992435")
                .setOrderAmount("1")
                .setOrderCurrency("CNY")
                .setNotifyUrl("http://XXX.hyd8.cn/123")
                .setCallbackUrl("https://jf.hyd8.cn")
                .setRemark("test")
                .setPaymentModeCode("ALIPAY-WAP-P2P")
                .setClientIp("113.87.117.16");

//        builder.setAppId("wx7fb6f04c804f1475");
//        builder.setOpenId("ozWu74oIKfQ9JRTSHtdzATkmmNCc");
        builder.setNeedOpenLink("1");
        builder.setReportSerialNo("89223946902001");
        builder.setPartnerId("891693175");

        //商品信息
        ProductDetail productDetail = new ProductDetail();
        productDetail.setName("测试1");
        productDetail.setQuantity(1L);
        productDetail.setAmount(1L);
        builder.addProductDetail(productDetail);


        builder.setPayer(new Payer());


        try {
            OnlinePayOrderExecuter executer = new OnlinePayOrderExecuter();
            System.setProperty("https.protocols", "TLSv1.2,TLSv1.1,SSLv3");
            executer.bothOrder(builder, new ResultListenerAdpater() {
                /**
                 * 提交成功，不代表支付成功
                 */
                @Override
                public void success(JSONObject jsonObject) {
                    log.info("success jsonObject:{}",jsonObject);
                    if (jsonObject.get("scanCode") != null) {
                        try {

                            OrderResponse orderResponse = new OrderResponse();
                            orderResponse.setHmac(jsonObject.getString("hmac"));
                            orderResponse.setMerchantId(jsonObject.getString("merchantId"));
                            orderResponse.setRequestId(jsonObject.getString("requestId"));
                            orderResponse.setStatus(jsonObject.getString("status"));

                            //
                            String scanCodeStr = jsonObject.getString("scanCode");
                            orderResponse.setScanCode(Base64.decodeBase64(scanCodeStr));

                        } catch (Exception e) {
                            log.error("{}",e);
                        }
                    }
                }

                @Override
                public void redirect(JSONObject jsonObject, String redirectUrl) throws IOException {
                    log.debug("redirectUrl:[" + redirectUrl + "]");
                }


            });
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public static void refund(){

        RefundBuilder builder = new RefundBuilder("892239469");
        builder.setRequestId("YB20241023113815039924955")
                .setAmount("1")
                .setOrderId("c8421f026947489f8361b056d9a9a297")
                .setRemark("")
                .setNotifyUrl("https://XXX.hyd8.cn/mall/order/notifyUpayRefund");

        builder.setPartnerId("891693175");

        OnlinePayOrderExecuter executer = new OnlinePayOrderExecuter();

        try {
            executer.bothRefund(builder, new ResultListenerAdpater() {
                @Override
                public void success(JSONObject jsonObject) {
                    log.info("refund 处理成功");
                    String result = jsonObject.toJSONString();
                    System.out.println(result);
                }

                @Override
                public void failure(JSONObject jsonObject) {
                    log.info("refund 处理失败");
                    String result = jsonObject.toJSONString();
                    System.out.println(result);
                }

                @Override
                public void pending(JSONObject jsonObject) {
                    log.info("refund 处理中");
                    String result = jsonObject.toJSONString();
                    System.out.println(result);
                }
            });
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    public static void query(){

        QueryBuilder builder = new QueryBuilder("892239469");
        builder.setRequestId("PAYE2024102117163833992498");
        builder.setPartnerId("891693175");


        OnlinePayOrderExecuter executer = new OnlinePayOrderExecuter();
        try {
            executer.bothQuery(builder, new ResultListenerAdpater() {
                @Override
                public void success(JSONObject jsonObject) {
                    log.info("处理成功,{}",jsonObject.toJSONString());
                }

                @Override
                public void failure(JSONObject jsonObject) {
                    log.info("处理失败,{}",jsonObject.toJSONString());
                }

                @Override
                public void pending(JSONObject jsonObject) {
                    log.info("待处理,{}",jsonObject.toJSONString());
                }
            });
        } catch (Exception e) {
           log.error("{}",e);
        }
    }

    public static void main(String[] args) {

        Map<String, Object> map = new HashMap<>();
        map.put("http.content.type.v3", "application/vnd.5upay-v3.0+json");
        map.put("http.content.type.v4", "application/vnd.5upay-v4.0+json");
        map.put("891693175.client.private.key.path", "d:\\hyd.pfx");
        map.put("891693175.client.private.key.password", "xxxx");
//        map.put("server.public.key.path", "d:\\server.cer");
        map.put("onlinepay.order.url", "https://apis.5upay.com/onlinePay/order");
        map.put("onlinepay.refund.url", "https://apis.5upay.com/onlinePay/refund");
        map.put("onlinepay.refund.query.url", "https://apis.5upay.com/onlinePay/refundQuery");
        map.put("onlinepay.cancelOrder.url", "https://apis.5upay.com/onlinePay/cancelOrder");
        map.put("onlinepay.query.url", "https://apis.5upay.com/onlinePay/query");

        ConfigurationUtils.setConfigSource(new MapConfiguration(map));
        System.setProperty("jdk.tls.disabledAlgorithms","");

        doUnifiedOrder();
//        refund();
//         query();
    }
}
